package com.example.administrator.mobliesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity {
    private View enterPswView;
    private View setupPswView;
    private EditText et_setupPsw;
    private EditText et_comfirmPsw;
    private SharedPreferences sharedPreferences;
    private GridView home_list;
    private MyAdapter myAdapter;
    private static  String[] names = {"手机防盗","通讯卫士","软件管理",
                                        "进程管理","流量统计","手机杀毒",
                                        "缓存清理","高级工具","设置中心"};
    private  static int[] ids = {R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,
            R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,
            R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        sharedPreferences = getSharedPreferences("config",MODE_PRIVATE);
        home_list = (GridView) findViewById(R.id.gv_list);
        myAdapter = new MyAdapter();
        home_list.setAdapter(myAdapter);
        home_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                switch (position){
                    case 0:
                        if (isSetupPsw()) {
                            showEnterDialog();
                        }else {
                            showSetupPswDialog();
                        }

                        break;
                    case 1:break;
                    case 2:break;
                    case 3:break;
                    case 4:break;
                    case 5:break;
                    case 6:break;
                    case 7:
                        intent = new Intent(HomeActivity.this,AToolActivity.class);
                        startActivity(intent);
                        break;
                    case 8:
                        intent = new Intent(HomeActivity.this,SettingActivity.class);
                        startActivity(intent);
                        break;
                    default:
                        break;

                }
            }

            
        });

    }
    private AlertDialog alertDialog;
    private void showSetupPswDialog() {
        AlertDialog.Builder builder =new AlertDialog.Builder(HomeActivity.this);
        setupPswView = View.inflate(HomeActivity.this, R.layout.setup_psw_dialog_layout, null);
        builder.setView(setupPswView);
        et_setupPsw =(EditText) setupPswView.findViewById(R.id.setup_psw);
        et_comfirmPsw = (EditText) setupPswView.findViewById(R.id.comfirm_psw);
        Button bt_setupPsw = (Button) setupPswView.findViewById(R.id.setup_psw_ok);
        Button bt_setupPswCancel = (Button) setupPswView.findViewById(R.id.setup_psw_cancel);
        bt_setupPswCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        bt_setupPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_setupPsw =(EditText) setupPswView.findViewById(R.id.setup_psw);
                et_comfirmPsw = (EditText) setupPswView.findViewById(R.id.comfirm_psw);
                String password = et_setupPsw.getText().toString().trim();
                String comfirmPsw = et_comfirmPsw.getText().toString().trim();
                if(TextUtils.isEmpty(password)||TextUtils.isEmpty(comfirmPsw)){
                    Toast.makeText(HomeActivity.this, "密码不能为空，请重新输入", Toast.LENGTH_SHORT).show();
                    et_setupPsw.setText("");
                    et_comfirmPsw.setText("");
                    return;
                }else if (!password.equals(comfirmPsw)){
                    Toast.makeText(HomeActivity.this, "请确认输入密码无误，请重新输入", Toast.LENGTH_SHORT).show();
                    et_setupPsw.setText("");
                    et_comfirmPsw.setText("");
                    return;
                }else {
                    Toast.makeText(HomeActivity.this, "设置密码成功", Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("password", password);
                    editor.commit();
                    alertDialog.dismiss();
                    return;
                }
            }
        });

        alertDialog = builder.show();
    }
    private EditText ed_enter_psw;
    private void showEnterDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        enterPswView = View.inflate(HomeActivity.this,R.layout.psw_enter_layout,null);
        builder.setView(enterPswView);
        ed_enter_psw = (EditText) enterPswView.findViewById(R.id.enter_psw);


        Button enterPswOk = (Button) enterPswView.findViewById(R.id.enter_psw_ok);
        Button enterPswCancel = (Button) enterPswView.findViewById(R.id.enter_psw_cancel);

        enterPswCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        enterPswOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = ed_enter_psw.getText().toString().trim();
                String passwordCom = sharedPreferences.getString("password",null).trim();
                if(password.equals(passwordCom)){
                    Toast.makeText(HomeActivity.this,"密码正确",Toast.LENGTH_SHORT).show();
                    alertDialog.dismiss();
                    Intent intent = new Intent(HomeActivity.this,LostFoundActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
                }else {
                    Toast.makeText(HomeActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    ed_enter_psw.setText("");
                    alertDialog.dismiss();
                    return;
                }

            }
        });



        alertDialog = builder.show();

    }
    private boolean isSetupPsw() {
        String password = sharedPreferences.getString("password",null);
        if (TextUtils.isEmpty(password)){
            return false;
        }else {
            return true;
        }
    }

    private  class  MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return  null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(HomeActivity.this, R.layout.list_item_home, null);
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
            TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
            tv_item.setText(names[position]);
            iv_item.setImageResource(ids[position]);
            return view;
        }
    }

}
